/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entity.Person;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Edison Parra
 */
public class PersonDao {

    /**Update the password for the person.
     * @param p The person object that will be updated
     * @param em EntityManager object to make the operation
     */
    public void updatePass(final Person p, final EntityManager em) {
        em.merge(p);
    }
    
    /**This method search a person by an userName.
     * @param username The username searched
     * @param em The entityManager
     * @return List<Person>
     */
    public List<Person> searchByUsername(final String username, final EntityManager em) {
        Query q = em.createNamedQuery("Person.findByUserName");
        q.setParameter("userName", username);
        List<Person> person = new ArrayList<Person>();
        person.add((Person) q.getSingleResult());
        return person;
    }

   /**This method saves the information of a person in the database.
     * @param p The person object that will be created
     * @param em EntityManager object to make the operation
     */
    public void create(final Person p, final  EntityManager em) {
        em.persist(p);
    }

    /**This method deletes tehe information of a person from the database.
     * @param p The person object that will be deleted
     * @param em EntityManager object to make the operation
     */
    public void delete(final Person p, final EntityManager em) {
        em.remove(em.merge(p));
    }
}
